﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Exam.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Web.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class SearchExamQuestion
    {
        public static string GetPluginJavascript(string SiteId)
        {
            string Html=
                "<script language=javascript>\r\n" +
                
                " function JumPageQuestion(PageIndex) \r\n" +
                " { \r\n" +
                "     document.getElementById('txtPageIndex').value=PageIndex;\r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML='Đang tìm kiếm...'; \r\n" +
                "     setTimeout(\"RealJumpPageQuestion()\",10); \r\n" +
                " } \r\n" +

                " function RealJumpPageQuestion() \r\n" +
                " { \r\n" +
                "     SiteId='" + SiteId + "';\r\n" +
                "     PageIndex=document.getElementById(\"txtPageIndex\").value; \r\n" +
                "     GroupQuestionId=document.getElementById(\"drpSelectGroupQuestion\").options[document.getElementById(\"drpSelectGroupQuestion\").selectedIndex].value; \r\n" +
                "     SubGroupQuestionName=document.getElementById(\"txtSubGroupQuestionName\").value; \r\n" +
                "     Type=document.getElementById(\"drpSelectType\").options[document.getElementById(\"drpSelectType\").selectedIndex].value; \r\n" +
                "     Keyword=document.getElementById(\"txtKeyword\").value; \r\n" +
                "     SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                
                "     ExamCollectionId=document.getElementById('txtSelectedExamCollectionId').value;\r\n" +
                "     retValue=Portal.Exam.Render.SearchExamQuestion.ServerSideSearch(SiteId, GroupQuestionId,SubGroupQuestionName,Type, Keyword,SelectLevel, ExamCollectionId, PageIndex).value; \r\n" +
                "     if(retValue.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(retValue); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML=\"\"; \r\n" +
                "     document.getElementById(\"divResultContent\").innerHTML=retValue; \r\n" +
                " } \r\n" +

               
                " function ClientSelectExamQuestion(QuestionId)\r\n"+
                " {\r\n"+
                "     ExamCollectionId=document.getElementById('txtSelectedExamCollectionId').value;\r\n" +
                "     AjaxOut=Portal.Exam.Render.SearchExamQuestion.ServerSideAddToCollection(SiteId,ExamCollectionId,  QuestionId).value;\r\n" +
                "     if(AjaxOut.Error)\r\n" +
                "     {\r\n" +
                "         alert(AjaxOut.InfoMessage);\r\n" +
                "         return;\r\n" +
                "     }\r\n" +
                "      document.getElementById('divQuestion'+QuestionId).innerHTML='';\r\n"+
                " }\r\n"+


                " function ResetSearch()\r\n" +
                "  {\r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML=\"\";\r\n" +
                "     document.getElementById(\"divResultContent\").innerHTML=\"\";\r\n" +
                "     document.getElementById(\"divResultInfo\").innerHTML=\"???\";\r\n" +
                " }\r\n" +


                "</script>\r\n";
            return Html;
        }

        public static AjaxOut Draw(string SiteId, string ExamCollectionId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string WebPartLayoutRoot = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);

                ExamCollectionCls OExamCollection = ExamCollectionBll.CreateExamCollectionModel(SiteId, ExamCollectionId);
                
                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, "");
                string SelectGroupQuestionText = "<select class=drplist id=drpSelectGroupQuestion onchange=javascript:DetectMonthi();>";
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    string GroupQuestionId = GroupQuestions[iIndex].GroupQuestionId;
                    string Name = GroupQuestions[iIndex].GroupQuestionName;
                    SelectGroupQuestionText += "<option value=\"" + GroupQuestionId + "\">" + Name + "</option>";
                }
                SelectGroupQuestionText += "</select>";

               
                string[] Types = new string[]{
                    "BestAnswer","TrueFalse","MultiChoice","CrossLink","Image","FreeText"
                };
                string[] SubjectTypes = new string[]{
                    "Câu hỏi 1 phương án đúng nhất","Câu hỏi dạng đúng hoặc sai","Câu hỏi hơn 1 phương án đúng","Câu hỏi liên kết","Câu hỏi hinh ảnh","Câu hỏi luận",
                };

                string SelectTypeText = "<select class=drplist id=drpSelectType onchange=javascript:JumPageQuestion(0);>";
                for (int iIndex = 0; iIndex < Types.Length; iIndex++)
                {
                    SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] +" ("+Types[iIndex]+")</option>";
                }
                SelectTypeText += "</select>";

                
                string SelectLevelText =
                    "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";


                string RetHtml =
                        " <div class=DatabaseFunction>\r\n" +
                        "        <ul>\r\n" +
                        "            <li onclick=\"javascript:JumPageQuestion(0);\"><img class=image src=\"" + IconRoot + "/Search.png\"><div>Tìm kiếm</div></li>\r\n" +
                        "            <li onclick=\"javascript:JumPageGroupQuestion(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                        "            <li onclick=\"javascript:BackFromSearchExamQuestion();\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                        "        </ul>\r\n" +
                        "  </div>\r\n" +
                        " <input id=txtPageIndex value=\"0\" type=hidden>\r\n" +
                        " <h3 style=\"text-align:left;\">Xác định câu hỏi cho bộ sưu tập - " + OExamCollection.ExamCollectionName + "</h3>\r\n" +
                        " <div class=CssFormSaveUpdate>\r\n" +
                        "   <table>" +
                        "       <tr>" +
                        "           <td class=Col1>Nhóm câu hỏi:</td>" +
                        "           <td class=Col2>" + SelectGroupQuestionText + "</td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Kiểu câu hỏi:</td>" +
                        "           <td class=Col2>" + SelectTypeText + "</td>" +
                        "       </tr>" +
                        "       <tr>" +
                        "           <td class=Col1>Mức độ:</td>" +
                        "           <td class=Col2>" + SelectLevelText + "</td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Phân loại:</td>" +
                        "           <td class=Col2><input id=txtSubGroupQuestionName type=textbox class=textbox></div></td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Tiêu đề câu hỏi: </td>" +
                        "           <td class=Col2><input class=textbox type=textbox id=txtKeyword></td>" +
                        "       </tr>" +
                        "   </table>" +
                        " </div>\r\n" +
                        "<div id=divProcessing></div>" +
                        "<div id=\"divResultContent\" class=\"tabcontent\">" +
                        "       <div class=CssNoData>Nhập các tiêu chí tìm kiếm và tiến hành tìm kiếm câu hỏi</div>" +
                        "</div>\r\n";



                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex) 
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage=ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        
        [AjaxPro.AjaxMethod]
        public static string ServerSideSearch(
            string SiteId, 
            string GroupQuestionId, 
            string SubGroupQuestionName, 
            string Type, 
            string Keyword, 
            string SelectLevel, 
            string ExamCollectionId, 
            int CurrentPageIndex)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                DataTable dtSearchExamQuestion = new QuestionBll().SearchQuestion(SiteId, GroupQuestionId, SubGroupQuestionName, Type, SelectLevel, ExamCollectionId, Keyword);
                ReturnPaging OReturnPaging=WebPaging.GetPaging(dtSearchExamQuestion.Rows.Count,CurrentPageIndex,50,10,"JumPageQuestion");
                string RetHtml=
                    OReturnPaging.PagingText+
                    "   <table style=\"width:100%\">" +
                    "       <tr>" +
                    "           <th>#</th>" +
                    "           <th>Câu hỏi</th>" +
                    "           <th>Nhóm</th>" +
                    "           <th style=\"text-align:right\">Điểm</th>" +
                    "           <th></th>" +
                    //"           <th></th>" +
                    //"           <th></th>" +
                    //"           <th></th>" +
                    "       </tr>";

                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    string QuestionId = (string)dtSearchExamQuestion.Rows[iIndex]["QuestionId"];
                    string QuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["Title"];
                    string RootGroupQuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["RootGroupQuestionName"];
                    string GroupQuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["GroupQuestionName"];
                    string DisplayText = GroupQuestionName;
                    if (!RootGroupQuestionName.Equals(GroupQuestionName))
                    {
                        DisplayText = RootGroupQuestionName + " / " + GroupQuestionName;
                    }
                    int MaxPoint = (int)dtSearchExamQuestion.Rows[iIndex]["MaxPoint"];
                    //string SelectUrl = "javascript:ClientSelectExamQuestion('" + QuestionId + "')";
                    //string MoveUrl = "javascript:ClientMoveQuestion('" + QuestionId + "')";
                    //string HtmlEditorUrl = "javascript:ShowClientHtmlEditorForm('" + QuestionId + "')";
                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex+1) + "</td>" +
                    "           <td>" + QuestionName + "</td>" +
                    "           <td>" + DisplayText + "</td>" +
                    "           <td class=tdIndex>" + MaxPoint.ToString("#,##0") + "</td>" +
                    //"           <td class=tdButton><a href=\"javascript:ShowClientUpdateUrlForm('" + QuestionId + "');\">Cập nhật</a></td>" +
                    "           <td style=\"width:100px;\"><div id=\"divQuestion" + QuestionId + "\"><input type=button class=button onclick=\"javascript:ClientSelectExamQuestion('" + QuestionId + "');\" value=\"Chọn câu hỏi này\"></div></td>" +
                    //"           <td class=tdButton><a href=\"" + MoveUrl + "\">Chuyển</a></td>" +
                    //"           <td class=tdButton><a href=\"" + HtmlEditorUrl + "\">HtmlEditor</a></td>" +
                    "       </tr>";
                }
                RetHtml +=
                    "   </table>\r\n"+
                    "</div>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }




        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddToCollection(
            string SiteId, 
            string ExamCollectionId,
            string QuestionId
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new QuestionBll().AddQuestionToExamCollection(null, SiteId, ExamCollectionId, QuestionId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
